package com.auto.learning.tools;

import android.annotation.SuppressLint;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateUtil {
	public static String type1 = "yyyy-MM-dd EE hh-mm-ss";
	public static String type2 = "yyyy-MM-dd hh:mm:ss";
	public static String type3 = "yyyy-MM-dd HH:mm";
	public static String type4 = "yyyy-MM-dd";
	public static String type5 = "HH:mm";

	public static String formatDuring(long mss) {
		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 ";
	}
	
	public static String formatDuring2(long mss) {
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return (hours > 0 ? (hours + "时") : "") + (minutes > 0 ? minutes + "分"  : "" ) + seconds + "秒 ";
	}
	
	public static String formatDuring3(long mss) {
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return (hours > 0 ? ("<font color=\"#2dd2a7\">" + hours+ "</font>" + " 时") : "") + (minutes > 0 ? "<font color=\"#2dd2a7\">" + minutes+ "</font>"   + " 分"  : "" ) + "<font color=\"#2dd2a7\">" + seconds+ "</font>"  + " 秒 ";
	}

	public static String getCurrTime_Type2() {
		SimpleDateFormat df = new SimpleDateFormat(type2);
		return df.format(new Date());
	}
	
	public static String convertToFormat(Date date){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	public static String getCurrDate() {
		SimpleDateFormat df = new SimpleDateFormat(type4);
		return df.format(new Date());
	}

	public static String getCurrTime() {
		SimpleDateFormat df = new SimpleDateFormat(type5);
		return df.format(new Date());
	}

	public static int getCurrDateHour() {
		// int hours = new Date().getHours();
		Calendar cd = Calendar.getInstance();
		return cd.get(Calendar.HOUR_OF_DAY);
	}

	public static String getCurrDatePrevious(int a) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -a); // 得到前一天
		Date date = calendar.getTime();
		SimpleDateFormat df = new SimpleDateFormat(type4);
		return df.format(date);
	}

	public static String getNextDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, +1);
		Date date = calendar.getTime();
		SimpleDateFormat df = new SimpleDateFormat(type4);
		return df.format(date);
	}
	
	public static String getPreviousYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, -1);
		Date date = calendar.getTime();
		SimpleDateFormat df = new SimpleDateFormat(type4);
		return df.format(date);
	}

	/**
	 * 计算某天到今天的天数
	 * 
	 * @param someDay
	 * @return
	 */
	public static int caculateDays(String someDay) {
		try {
			String currentDay = DateUtil.getCurrDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date d1 = sdf.parse(someDay);
			Date d2 = sdf.parse(currentDay);
			int days = (int) Math
					.abs(((d1.getTime() - d2.getTime()) / (24 * 3600 * 1000)));
			return days;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}

	/**
	 * 获取当前系统+1天的时间
	 * 
	 * @return
	 */
	public static String getTomorrowTime() {
		Calendar calendar = Calendar.getInstance();
		long nowTime = calendar.getTimeInMillis();
		long tomorrowTime = nowTime + 86400000;// 当前时间+86400000毫秒（一天）
		calendar.setTimeInMillis(tomorrowTime);
		DateFormat formatter = new SimpleDateFormat(type3);
		return formatter.format(calendar.getTime());
	}

	// 获取当前系统时间
	public static String getCurrDateTime() {
		SimpleDateFormat df = new SimpleDateFormat(type3);
		return df.format(new Date());
	}

	/**
	 * 截取日期中的年、月、日
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static String getDateYearMonDay(String startDate, String endDate) {

		String dateString = null;
		if ((startDate == null || startDate.equals(""))
				|| (endDate == null || endDate.equals(""))) {
			return dateString;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(type4);
		try {

			Date parseStartDate = sdf.parse(startDate);
			Date parseEndDate = sdf.parse(endDate);
			Calendar startCalender = Calendar.getInstance();
			startCalender.setTime(parseStartDate);
			int startYear = startCalender.get(Calendar.YEAR);
			int startMonth = startCalender.get(Calendar.MONTH) + 1;
			int startDay = startCalender.get(Calendar.DAY_OF_MONTH);

			Calendar endCalender = Calendar.getInstance();
			endCalender.setTime(parseEndDate);
			int endYear = endCalender.get(Calendar.YEAR);
			int endMonth = endCalender.get(Calendar.MONTH) + 1;
			int endDay = endCalender.get(Calendar.DAY_OF_MONTH);

			if (parseStartDate.getTime() == parseEndDate.getTime()) {
				dateString = startMonth + "/" + startDay;
			} else {
				dateString = startMonth + "/" + startDay + "~" + endMonth + "/"
						+ endDay;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateString;
	}

	/**
	 * 将时间字符串转换成Date对象
	 * @param
	 * @return
	 */
	public static Date strConvertDate(String someDay) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(type3);
			Date d1 = sdf.parse(someDay);
			return d1;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date strConvertDate2(String someDay) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(type4);
			Date d1 = sdf.parse(someDay);
			return d1;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 比较两个时间
	 * @param date
	 * @return
	 */
	public static boolean comparaDate(Date date,Date date2){
		Date currentDate=new Date();
		Log.d("date2", currentDate.after(date) +"--"+ currentDate.before(date2));
		return currentDate.after(date) && currentDate.before(date2);
	}
	
	/**
	 * 比较两个时间
	 * @param date
	 * @return
	 */
	public static boolean comparaDate(Date date){
		Date currentDate=new Date();
		return currentDate.after(date);
	}
	
	public static int getWeekOfDate(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return w;
	}
}
